package com.example.myswiperefreshlayoutdemo;

import androidx.appcompat.app.AppCompatActivity;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.SimpleTimeZone;

public class MainActivity extends AppCompatActivity {
    private SwipeRefreshLayout swipeRefreshLayout;
    private String[] names = new String[]{"Lion","Tiger","Monkey","Cat","Dog","Elephant"};

    @SuppressLint("ResourceAsColor")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //创建list 集合
        ListView list = findViewById(R.id.aa);
        List<Map<String,Object>> listItems = new ArrayList<>();
        for(int i=0;i<names.length;i++) {
            Map<String,Object> listItem = new HashMap<>();
            listItem.put("name",names[i]);
            listItems.add(listItem);
        }

        SimpleAdapter simpleAdapter = new SimpleAdapter(this,listItems,R.layout.item,new String[]{"name"},new int[]{R.id.names});
        list.setAdapter(simpleAdapter);


        //
       final SwipeRefreshLayout swip_refresh_layout = findViewById(R.id.swipeLayout);
        swip_refresh_layout.setColorSchemeColors(R.color.colorPrimary); //可以改变下拉刷新时的颜色
        swip_refresh_layout.setProgressBackgroundColorSchemeColor(R.color.colorPrimaryDark);//设置下拉进度的背景颜色
        //
        swip_refresh_layout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        swip_refresh_layout.setRefreshing(false); //设置刷新状态，false代表停止执行

                    }
                },2000);
            }
        });
    }
}